/*
 * Galaxium Messenger
 * 
 * Copyright (C) 2008 Paul Burton <paulburton89@gmail.com>
 * 
 * License: GNU General Public License (GPL)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Collections.Generic;
using System.Security.Cryptography;

using Galaxium.Core;

using Anculus.Core;

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Galaxium.Protocol.Facebook
{
	public sealed class FBSession : AbstractSession
	{
		private FBConnection _connection;
		
		private ContactCollection _contacts;
		private GroupCollection _groups;
		private FBGroup _empty_group;
		
		public override GroupCollection GroupCollection
		{
			get { return this._groups; }
		}

		public override ContactCollection ContactCollection
		{
			get { return this._contacts; }
		}

		public IEnumerable<IGroup> Groups
		{
			get { return this._groups; }
		}
		
		public FBSession (FBAccount account) : base (account)
		{
			_contacts = new ContactCollection ();
			_groups = new GroupCollection ();
			
			Conversations = new FBConversationManager ();
			
			_empty_group = new FBGroup (this, "0", "Friends", true);
			
			_groups.Add (_empty_group);
			
			account.Presence = account.InitialPresence;
			
#pragma warning disable
			ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy ();
#pragma warning restore
			
			System.Net.ServicePointManager.Expect100Continue = false;
			
			FBConnectionInfo connectionInfo = new FBConnectionInfo ("www.facebook.com");
			connectionInfo.HostName = "www.facebook.com";
			
			_connection = new FBConnection (this);
		}
		
		protected override void Dispose (bool disposing)
		{
			
		}
		
		public override void Connect ()
		{
			_connection.Connect();
			
			OnConnected (new SessionEventArgs (this));
		}
		
		public override void Disconnect ()
		{
			OnDisconnected (new SessionEventArgs (this));
		}
		
		public override void SetPresence (BasePresence presence)
		{
		}
		
		protected override void OnLoginCompleted (SessionEventArgs args)
		{
			base.OnLoginCompleted (args);
		}
		
		/*void SyncAddressBook ()
		{
			try
			{
				HttpWebRequest req = CreateHttpRequest ("http://www.new.facebook.com/ajax/chat/buddy_list.php");
				req.Method = "POST";
				req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
				
				byte[] postData = Encoding.UTF8.GetBytes (string.Format ("user={0}&popped_out=false&force_render=true&buddy_list=1", (Account as FBAccount).ProfileID));
				
				req.ContentLength = postData.LongLength;
				
				Stream contentStream = req.GetRequestStream ();
				contentStream.Write (postData, 0, postData.Length);
				contentStream.Close ();
				
				req.BeginGetResponse (SyncComplete, req);
			}
			catch (WebException ex)
			{
				Log.Error (ex, "Unable to retrieve address book");
				
				EmitErrorOccurred (new ErrorEventArgs (this, "Login Failed", "Synchronization Failed!"));
			}
		}
		
		void SyncComplete (IAsyncResult ar)
		{
			try
			{
				HttpWebRequest req = (HttpWebRequest)ar.AsyncState;
				HttpWebResponse response = (HttpWebResponse)req.EndGetResponse(ar);
				Stream stream = response.GetResponseStream ();
				
				if (response.ContentLength > 0)
				{
					Log.Debug ("Received {0} byte {1} response", response.ContentLength, response.ContentType);
					
					byte[] data = new byte[response.ContentLength];
					int done = 0, read;
					
					while ((read = stream.Read (data, done, (int)Math.Min (data.Length - done, 256))) > 0)
						done += read;
					
					string str = Encoding.UTF8.GetString (data);
					
					Log.Debug ("Received sync response:\n{0}", str);
				}
				else
					Log.Debug ("No sync response");
				
				LogCookies (new Uri ("https://login.facebook.com/login.php"));
			}
			catch (Exception ex)
			{
				Log.Error (ex, "Unable to read sync result");
				
				EmitErrorOccurred (new ErrorEventArgs (this, "Synchronization Failed", "Connection Failed!"));
			}
		}*/
		
		
	}
}